<script>
/*
 * @Author: Your name
 * @Date:   2020-07-30 09:30:51
 * @Last Modified by:   Your name
 * @Last Modified time: 2020-07-30 12:46:47
 */
</script>
<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="~assets/img/1.jpg" alt="" />
      </div>
      <div class="login-form">
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="0px"
        >
          <el-form-item prop="username">
            <el-input
              v-model="ruleForm.username"
              prefix-icon="iconfont icon-yonghu1"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="ruleForm.password"
              prefix-icon="iconfont icon-mima"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="success" plain @click="login('ruleForm')"
              >登录</el-button
            >
            <el-button type="info" plain @click="resetForm('ruleForm')"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { request } from 'network/request.js'
export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在6-16个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  methods: {
    // 点击重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 点击登录
    login(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        const result = await request({
          url: '/login',
          method: 'post',
          params: this.ruleForm
        })
        console.log(result)
        if (result.meta.status !== 200)
          return this.$message.error('登陆失败，账号或用户名有误')
        this.$message.success('登录成功')
        const token = result.data.token
        window.localStorage.setItem('logintoken', token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  height: 100%;
  width: 100%;
  background-color: #2b4b6b;
  position: relative;
  .login-box {
    position: absolute;
    width: 400px;
    height: 300px;
    left: 50%;
    top: 50%;
    background: #ffffff;
    transform: translate(-50%, -60%);
    .avatar-box {
      width: 110px;
      height: 110px;
      background: #ffffff;
      padding: 10px;
      border-radius: 50%;
      margin: -50px auto;
      box-shadow: 0px 0px 7px #ffffff;
      border: 1px solid #cccccc;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login-form {
      width: 100%;
      box-sizing: border-box;
      padding: 20px;
      position: absolute;
      bottom: 0px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
